﻿using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_TransportBillGroupBusiness : BaseBusiness<PB_TransportBillGroup>, IPB_TransportBillGroupBusiness, ITransientDependency
    {
        public PB_TransportBillGroupBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_TransportBillGroup>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_TransportBillGroup>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_TransportBillGroup, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_TransportBillGroup> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_TransportBillGroup data)
        {
            await InsertAsync(data);
        }
        public async Task AddDataAsync(List<PB_TransportBillGroup> data)
        {
            await InsertAsync(data);
        }
        public async Task UpdateDataAsync(PB_TransportBillGroup data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }
        public async Task DeleteOtherIdDataAsync(object where)
        {
            await DeleteOtherIdAsync(where);
        }
        public async Task AddDatainfo(PB_TransportBillGroup data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDatainfo(PB_TransportBillGroup data)
        {
           await UpdateAsync(data);
        }

        /// <summary>
        /// 初始化PB_TransportBillGroup表数据
        /// 只作用2022-03更新生产库使用
        /// </summary>
        /// <returns></returns>
        public async Task RechargeBillGroupAsync()
        {
            string sqlMain = @"select CreateTime,CreatorId,Id,DeliveryNoticeId,Deleted from PB_TransportBill where Id NOT in (select ParentId from PB_TransportBillGroup)";
            var dtdata = await Db.GetDataTableWithSqlAsync(sqlMain);
            if (dtdata.Rows.Count > 0)
            {
                List<PB_TransportBillGroup> listdata = new List<PB_TransportBillGroup>();
                foreach (DataRow dr in dtdata.Rows)
                {
                    PB_TransportBillGroup groupdata = new PB_TransportBillGroup();
                    groupdata.Id = IdHelper.GetId();
                    groupdata.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
                    groupdata.CreatorId = dr["CreatorId"].ToString();
                    groupdata.ParentId = dr["Id"].ToString();
                    groupdata.DeliveryNoticeId = dr["DeliveryNoticeId"].ToString();
                    groupdata.Deleted = false;
                    listdata.Add(groupdata);
                }
                await Db.InsertAsync<PB_TransportBillGroup>(listdata);
            }
            else
            {
                throw new BusException("暂无数据");
            }
        }
        #endregion

        #region 私有成员

        #endregion
    }
}